import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs';
import {UserService} from '../api/user.service';

@Injectable({
  providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
  constructor(private router: Router, private user: UserService) {
  }

  check(): Observable<boolean> {
    return new Observable((observer) => {
      observer.next(true);
      // this.user.isAuthenticated().subscribe(() => {
      //   observer.next(true);
      // }, () => {
      //   observer.next(false);
      //   this.router.navigate(['login']).catch(reason => {
      //     console.log(reason);
      //   });
      // });
    });
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    return this.check();
  }
}
